<template>
  <el-dialog
    :title="title"
    class="gl_dialog_TitleLine gl_dl_footer"
    :visible.sync="dialogVisible"
    :before-close="close"
    width="660px"
    append-to-body
    :close-on-click-modal="false"
  >
    <div class="gl_mb_15">
      <el-form ref="form" class="gl_form_mini" :rules="rules" style="padding: 0 30px" label-width="100px" :model="formData">
        <el-form-item prop="name" label="教室名称">
          <el-input v-model="formData.name" placeholder="请输入教室名称" size="mini" clearable maxlength="16" show-word-limit />
        </el-form-item>
        <el-form-item prop="address" label="详细地址">
          <el-input v-model="formData.address" placeholder="请输入详细地址" size="mini" clearable />
        </el-form-item>
        <el-form-item prop="maxStudentCount" label="可容纳人数">
          <el-input v-model="formData.maxStudentCount" placeholder="请输入可容纳人数" size="mini" clearable>
            <template slot="append">人</template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="close()">取 消</el-button>
      <el-button :loading="loading" size="mini" type="primary" @click="addOrEdit()">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapState } from 'vuex';
import validator from '@/lib/validate/index.js';
import {
  api_POST_ClubClassroom, // 新增
  api_PUT_ClubClassroomId // 修改
} from '@/extend/api/hallswaggerApi/app.js';
export default {
  props: {
    title: { // 弹窗标题
      type: String,
      default: ''
    },
    code: { // 弹窗标题
      type: String,
      default: ''
    },
    formData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      id: '', // 编辑时使用的id
      rules: {
        name: [
          validator.required('教室名称不能为空')
        ],
        address: [
          validator.required('详细地址不能为空')
        ],
        maxStudentCount: [
          validator.required('可容纳人数不能为空'),
          {
            validator(rule, value, callback) {
              callback(validator.checkNumber(value, '请输入整数数字'));
            },
            trigger: 'blur'
          },
          {
            validator(rule, value, callback) {
              callback(validator.checkNumberLen(value, '人数不可大于999999999'));
            },
            trigger: 'blur'
          }
        ]
      }
    };
  },
  computed: {
    ...mapState({
      DelegationId: state => state.user.rootDelegationId
    })
  },
  methods: {
    // 打开弹窗
    open(id) {
      this.dialogVisible = true;
      this.id = id;
    },
    // 关闭弹窗
    close() {
      this.$refs['form'].resetFields();
      this.dialogVisible = false;
    },
    // 新建 or 编辑
    addOrEdit() {
      var that = this;
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.loading = true;
          if (that.title === '新建教室') {
            api_POST_ClubClassroom(that.formData).then(res => {
              that.loading = false;
              that.$refs['form'].resetFields();
              that.dialogVisible = false;
              if (that.code) {
                that.$emit('refreshRoom');
              } else {
                that.$parent.getList();
              }
              that.$message({ type: 'success', message: '新建成功!' });
            }).catch(error => {
              that.loading = false;
              that.$message({ type: 'error', message: '新建教室失败 ' + error.error.message });
            });
          } else if (that.title === '编辑教室') {
            api_PUT_ClubClassroomId(that.id, that.formData).then(res => {
              that.loading = false;
              that.$refs['form'].resetFields();
              that.dialogVisible = false;
              that.$parent.getList();
              that.$message({ type: 'success', message: '编辑成功!' });
            }).catch(error => {
              that.loading = false;
              that.$message({ type: 'error', message: '编辑失败 ' + error.error.message });
            });
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
